package com.mofei.h003test.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.mofei.h003test.R;
import com.mofei.h003test.adapter.ScanResultsAdapter;
import com.mofei.h003test.presenter.MainActivityPresenter;
import com.polidea.rxandroidble2.scan.ScanResult;

public class MainActivity extends AppCompatActivity {


    private ScanResultsAdapter mScanResultsAdapter;
    private RecyclerView mRecyclerView;
    private MainActivityPresenter mPresenter;
    private Button mBtnScanDevice;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initData();
    }

    private void initView() {

        mBtnScanDevice = findViewById(R.id.scan_device);
        mBtnScanDevice.setOnClickListener(mOnClickListener);

        mRecyclerView = findViewById(R.id.scan_results);

        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setItemAnimator(null);

        LinearLayoutManager recyclerLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(recyclerLayoutManager);
        mScanResultsAdapter = new ScanResultsAdapter();
        mRecyclerView.setAdapter(mScanResultsAdapter);
        mScanResultsAdapter.setOnAdapterItemClickListener(mOnAdapterItemClickListener);

    }

    private void initData() {
        mPresenter = new MainActivityPresenter(this);
    }

    private View.OnClickListener mOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int id = view.getId();
            if (id == R.id.scan_device) {
                mBtnScanDevice.setEnabled(false);
                mScanResultsAdapter.clearScanResults();
                mPresenter.startScanDevice();
            }
        }
    };

    public void showToast(String message) {
        mBtnScanDevice.setEnabled(true);
        Toast.makeText(this, "" + message, Toast.LENGTH_SHORT).show();
    }

    private ScanResultsAdapter.OnAdapterItemClickListener mOnAdapterItemClickListener = new ScanResultsAdapter.OnAdapterItemClickListener() {
        @Override
        public void onAdapterViewClick(View view) {
            final int childAdapterPosition = mRecyclerView.getChildAdapterPosition(view);
            final ScanResult itemAtPosition = mScanResultsAdapter.getItemAtPosition(childAdapterPosition);
            onAdapterItemClick(itemAtPosition);
        }
    };

    private void onAdapterItemClick(ScanResult scanResults) {
        final String macAddress = scanResults.getBleDevice().getMacAddress();
        ControlActivity.start(this, macAddress);
        mBtnScanDevice.setEnabled(true);
    }

    public void addScanResult(ScanResult scanResult) {
        mScanResultsAdapter.addScanResult(scanResult);
    }

    @Override
    protected void onPause() {
        super.onPause();
        mPresenter.onPause();
    }
}
